<?php
// $Id$

include_once('fpdf.php');
include_once('AssessmentModel.class.php');
include_once('PaymentsModel.class.php');
include_once('ReservationModel.class.php');
include_once('Words.php');
class PDFModel extends FPDF
{

	function print_payment($payment_id){

 	    $pm  = new PaymentsModel('PDFModel');
        $am  = new AssessmentModel('PDFModel');
		$nw  = new Numbers_Words();
		$payment =  $pm->payment_byid($payment_id);
		if (!$payment) die('<script>window.close();</script>');
		$payment_details = $pm->payment_details($payment_id);
		$sinfo = $am->student_info($payment['enrollment_id']);

		$date = date("M d, Y",strtotime($payment['created_date']));
		$name = $sinfo['stud_no'] . " / " . $sinfo['level'] . " / ". $sinfo['name']; //. ", " $sinfo['fname'] . " "  . $sinfo['lname']	

		$total = $tuition = $misc = $ofees = 0;
		
		foreach ($payment_details as $d ) {
			$total += $d['amount'];
			if ($d['detail_type'] == 'other_fees'){
				$ofees += $d['amount'];
			}elseif (strtolower($d['item']) == 'tuitionfee'){
				$tuition += $d['amount'];
			}elseif (strtolower($d['item']) == 'miscellaneous'){
				$misc += $d['amount'];
			}
		}

		$b = 0;

		//$this=new FPDF();
		$this->AddPage();

		// this is obvious
        $this->SetFont('Arial', '', 10);

		// Set the initial position
		$this->SetY(8);
        //$this->SetX(178);
		$this->cell(160);

		// create a cell param is width, height, txt, border(1,0),line(0,1,2),align(L,C,R)
		$this->Cell(25,5,$date, $b, 0, 'L');

		//Line break
		$this->Ln(20);

		//Move to 50 cm to the right
		$this->cell(55);

		// display the name
        $this->Cell(100,5,$name, $b, 0, 'L');

		$this->Ln(5);
		$this->cell(38);
        $this->Cell(100,5,strtoupper($nw->toCurrency($total)), $b, 0, 'L');

		
		$this->Ln(7);
		$this->cell(38);
        $this->Cell(100,5,$payment['description'], $b, 0, 'L');


        // distance of tuition fee from the left
		$l = 33;
        // distance of cash bank and check from the left
		$l2 = 67;
		$w = 21;
		//display the particulars	
		$this->Ln(5);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,number_format($tuition,2), $b, 0, 'R');
		$this->cell($l2);
        $this->Cell($w,3,$payment['amount_tendered'], $b, 0, 'L');

		$this->Ln(5);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,number_format($misc,2), $b, 0, 'R');
		$this->cell($l2);
        $this->Cell($w,3,$payment['check_no'], $b, 0, 'L');

		$this->Ln(5);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,number_format($ofees,2), $b, 0, 'R');
		$this->cell($l2);
        $this->Cell($w,3,$payment["bank"], $b, 0, 'L');

		$this->Ln(10);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,number_format($total,2), $b, 0, 'R');

		$this->Ln(5);
		$this->cell(25);
        $this->Cell(50,5,"Assessor: {$payment['created_by']}", $b, 0, 'R');
		$this->cell(80);
        $this->Cell($w,5,$payment['or_num'], $b, 0, 'R');

		$this->Output('payment.pdf','D');
        die();
	}
	function print_reservation($reservation_id){
		$rm     = new ReservationModel('ReservationAction');
		$nw  = new Numbers_Words();

		$detail = $rm->detail2($reservation_id);
		if (!$detail) die('<script>window.close();</script>');
		$date = date("M d, Y",strtotime($detail['created_date']));
		$b = 0;

		$name = $detail['stud_no'] . " / ". $detail['name']; 

		//$this=new FPDF();
		$this->AddPage();

		// this is obvious
        $this->SetFont('Arial', '', 10);

		// Set the initial position
		$this->SetY(8);
        //$this->SetX(178);
		$this->cell(160);

		// create a cell param is width, height, txt, border(1,0),line(0,1,2),align(L,C,R)
		$this->Cell(25,5,$date, $b, 0, 'L');

		//Line break
		$this->Ln(20);

		//Move to 50 cm to the right
		$this->cell(55);

		// display the name
        $this->Cell(100,5,$name, $b, 0, 'L');

		$this->Ln(5);
		$this->cell(38);
        $this->Cell(100,5,strtoupper($nw->toCurrency($detail['amount_due'])), $b, 0, 'L');

		
		$this->Ln(7);
		$this->cell(38);
        $this->Cell(100,5,'Reservation', $b, 0, 'L');


        // distance of tuition fee from the left
		$l = 33;
        // distance of cash bank and check from the left
		$l2 = 67;
		$w = 21;
		//display the particulars	
		$this->Ln(5);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,'', $b, 0, 'R');
		$this->cell($l2);
        $this->Cell($w,3,$detail['amount_tendered'], $b, 0, 'L');

		$this->Ln(5);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,'', $b, 0, 'R');
		$this->cell($l2);
        $this->Cell($w,3,'', $b, 0, 'L');

		$this->Ln(5);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,number_format($detail['amount_due'],2), $b, 0, 'R');
		$this->cell($l2);
        $this->Cell($w,3,'', $b, 0, 'L');

		$this->Ln(10);
		$this->cell($l);
        $this->Cell($w,3,"", $b, 0, 'L');
        $this->Cell($w,3,number_format($detail['amount_due'],2), $b, 0, 'R');

		$this->Ln(5);
		$this->cell(25);
        $this->Cell(50,5,"Assessor: {$detail['created_by']}", $b, 0, 'R');
		$this->cell(80);
        $this->Cell($w,5,$detail['or_num'], $b, 0, 'R');

		$this->Output('payment.pdf','D');
        die();
	}
}
